<?php

/**
 * @param string $appkey
 * @param string $loginkey
 * @return string 空中网跳转地址
 */
function getGameUrl ($url,$appkey, $loginkey){
//    $url = "https://gctest205.ko10.cn/lyunify/loginlygameurl/";
    //$url = "https://gc.ko10.cn/h5/loginlygameurl/";
//    $url = "https://gc.ko10.cn/h5/loginlygameurl/";
    $ly = "wozan";
    $user_info = session('user_info');
    if (!empty($user_info)) {
        $user = json_decode($user_info, true);
        $uid = $user['id'];
        $uname = 'wozan'.$user['id'];
        $nname = urlencode($user['nick_name']);
        $ptime = date('Y-m-d H:i:s');
        $cm = 1;
        $pfType = "android";
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
            $pfType = "ios";
        }
        $sign = strtolower(md5($appkey.$ly.$uid.$uname.$ptime.$cm.$loginkey));
    
        $param_arr = array ('appkey'=>$appkey,'ly'=>$ly,'uid'=>$uid,'uname'=>$uname,'nname'=>$nname,
            'ptime'=>$ptime,'cm'=>$cm,'pfType'=>$pfType,'sign'=>$sign);
        $param = json_encode($param_arr);
        $key = base64_encode($param);
//        $url = $url.'/'.$ly.'/'.$key;
        $url = $url.'/'.$key;
        return $url;
    }
    return '';
    
}

/**
 * h5 游戏地址
 *
 */
function getH5GameUrl ($c_id,$game_id, $url, $appkey='', $loginkey=''){
    $user_info = session('user_info');
    if (!empty($user_info)) {
        //sign =MD5(u_id=u_id&g_id=g_id&c_id=c_id&c_time=c_time&key=key)
        $user = json_decode($user_info, true);
        $u_id = $user['id'];
        $g_id = $game_id;
        $c_id = $c_id;
        $c_time = time();
        $sign = md5("u_id=$u_id&g_id=$g_id&c_id=$c_id&c_time=$c_time&key=$appkey");
        $extra = json_encode(array('param1'=>'value1','param2'=>'value2'));
        $data = array(
            'u_id='.$u_id,
            'g_id='.$g_id,
            'c_id='.$c_id,
            'c_time='.$c_time,
            'extra='.urlencode($extra),
            'sign='.$sign
        );
        if (strstr($url,'?')) {
            return $url.'&'.implode('&',$data);
        } else {
            return $url.'?'.implode('&',$data);
        }
    }
    return '';
}
/**
 * 获取当前页面完整URL地址
 */
function get_url($protocal = true) {
    $sys_protocal = '';
    if ($protocal) {
        $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
    }
    $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
    $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
    $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
    return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
}

/**
 * @param $true 真实量
 * @param $show 显示量
 * @param $max_v 扣量临界值
 * @param $bi 扣量比例
 * @param $step 自增值
 */
function getKouStep ($true, $show, $max_v,$bi, $step=1){
    if (empty($bi)) return $step; // 如果比为0，则代表不扣量
    if (($true+$step) <= $max_v) {return $step;}
    $bi = $bi/1000;
    $idend = $true ? $true : 1 ;
    $true_bi = ($true - $show ) / $idend;
    if ($true_bi<$bi) { // 直接扣，没的说
        return  $step-floor($step * $bi);
    }
    return $step;
}


/**
 * 统一接口回调
 */
function notifyH5Game($trade, $game){
    $url = $game['reurl'];
    $trade_no = $trade['trade_no']; // 订单号
    $amount = $trade['pay'];
    $g_id = $trade['game_id']; // 游戏ID
    $gparams = urlencode($trade['callback_parms']);
    $key= $game['appkey'];
    $sign = md5("trade_no=$trade_no&amount=$amount&g_id=$g_id&gparams=$gparams&key=$key");
    \Think\Log::record('game----md5:'."trade_no=$trade_no&amount=$amount&g_id=$g_id&gparams=$gparams&key=$key".'    -----   md5:'.$sign);
    $get_url = $url."?trade_no=$trade_no&amount=$amount&g_id=$g_id&gparams=$gparams&sign=$sign";
    $rst = array(
        'result' => false,
        'url' => $get_url
    );
    try{
        $content = file_get_contents($get_url);
        $tmp = json_decode($content, true);
        if (isset($tmp['status']) && strtolower($tmp['status']) == 'success') {
            $rst['result'] = true;
        }
        \Think\Log::record('call back game:'.$content);
        $rst['info'] = $content;
    } catch (Exception $exception) {
        \Think\Log::record('call back game exception: '.$exception->getMessage());
        $rst['info'] = $exception->getMessage();
    }
    return $rst;
}


/**
 * 空中网回调方法
 */
function notifyKZW ($trade,$game){
    $rst = array( // 返回值
        'result' => false,
        'url' => ''
    );
    try{
        $params = getParams($game['extra_key']);
        $callback_params = json_decode($trade['callback_parms'], true);
        $debug = isset($params['debug'])? (int) $params['debug'] : 0; // Int 是否调试链接 0-测试;1-正式
        $url = $game['reurl'];

        $appkey = $game['appkey'];
        $ly = 'wozan';
        $uid = $trade['player_id'];
        $uname = $callback_params['username'];
        $serverId = $callback_params['serverid'];
        $ptime = date('Y-m-d H:i:s');
        $orderno = $trade['trade_no']; // 订单号
        $price = round($trade['pay']/100);
        $num = $callback_params['ybnum'];
        $yuanoritem = $callback_params['yborItem'];
        $itemId = $callback_params['itemId'];
        $itemName = $callback_params['itemName'];
        $itemNum = $callback_params['itemNum'];
        $extraparam = $callback_params['extraparam'];
        $pay_key = $params['paykey'];

        //示例: md5 (appkey+ly+uid+uname+serverId+ptime+orderno+price+num+yuanoritem+itemId+itemName+itemNum+extraparam+ PAY_KEY)
        $sign_val = urldecode($appkey.$ly.$uid.$uname.$serverId.$ptime.$orderno.$price.'.00'.$num.$yuanoritem.$itemId.$itemName.$itemNum.$extraparam.$pay_key);
        $sign = md5($sign_val);

        \Think\Log::record('game--kzw--md5:'.$sign_val.'    -----   md5:'.$sign);

        $param_arr = array ('appkey'=>$appkey,'ly'=>$ly,'uid'=>$uid,'uname'=>$uname,'serverId'=>$serverId,'ptime'=>$ptime,
            'orderno'=>$orderno, 'price' => $price, 'num' =>$num, 'yuanoritem'=>$yuanoritem, 'itemId'=>$itemId,
            'itemName'=>$itemName, 'itemNum'=>$itemNum, 'extraparam' =>$extraparam, 'debug'=>$debug,
            'sign'=>$sign);

        \Think\Log::record('game-kzw-opts'.json_encode($param_arr));
        $param = json_encode($param_arr);
        $key = base64_encode($param);
        $get_url = $url.'/'.$key;
//        $get_url = $url.$ly.'/'.$key;
        $rst['url'] = $get_url;
        $content = file_get_contents($get_url);
        $tmp = json_decode($content, true);
        if (isset($tmp['state']) && $tmp['state']) {
            $rst['result'] = true;
        }
        \Think\Log::record('call back kzw game:'.$content);
        $rst['info'] = $content;
    }catch (Exception $exception) {
        \Think\Log::record('call back game exception: '.$exception->getMessage());
        $rst['info'] = $exception->getMessage();
    }
    return $rst;
}